from fastapi import APIRouter, Depends
from loguru import logger

from app.schemas.plot_defect import *
from app.schemas.action import AttachFileDTO
from app.schemas.response import DefaultResponse
from utils.thread_pool import get_executor
from concurrent.futures import ThreadPoolExecutor
import app.services.plot_defect_service as plot_defect_service
import app.services.yolov8_service as yolov8_service
router = APIRouter(prefix='/image')


@router.post('/plot-defect', response_model=DefaultResponse)
async def plot_defect(request: PlotDefect, executor: ThreadPoolExecutor = Depends(get_executor)):
    # logger.info(f'[web-task] handle image plot-defect, request:{request.model_dump()}')
    def background_task():
        try:
            plot_defect_service.plot(request)
        except Exception as e:
            logger.error(f"Error in background task: {e}", exc_info=True)

    executor.submit(background_task)
    return DefaultResponse()

@router.post('/car-plate', response_model=DefaultResponse)
def car_plate(request: AttachFileDTO):
    # logger.info(f'[web-task] handle image car-plate, request:{model.model_dump()}')
    ocr_list = yolov8_service.predict_car_plate(request)
    return DefaultResponse(data=ocr_list)